package com.ruoyi.recipe.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.recipe.mapper.RecipeDishMapper;
import com.ruoyi.recipe.domain.RecipeDish;
import com.ruoyi.recipe.service.IRecipeDishService;
import com.ruoyi.common.core.text.Convert;

/**
 * 菜品Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-22
 */
@Service
public class RecipeDishServiceImpl implements IRecipeDishService {
    @Autowired
    private RecipeDishMapper recipeDishMapper;

    /**
     * 查询菜品
     *
     * @param dishId 菜品主键
     * @return 菜品
     */
    @Override
    public RecipeDish selectRecipeDishByDishId(Long dishId) {
        return recipeDishMapper.selectRecipeDishByDishId(dishId);
    }

    /**
     * 查询菜品列表
     *
     * @param recipeDish 菜品
     * @return 菜品
     */
    @Override
    public List<RecipeDish> selectRecipeDishList(RecipeDish recipeDish) {
        return recipeDishMapper.selectRecipeDishList(recipeDish);
    }

    @Override
    public List<RecipeDish> selectRecommendedRecipeDish(RecipeDish recipeDish) {
        return recipeDishMapper.selectRecommendedRecipeDish(recipeDish);
    }

    @Override
    public List<RecipeDish> selectPopularRecipeDish(RecipeDish recipeDish) {
        return recipeDishMapper.selectPopularRecipeDish(recipeDish);
    }

    @Override
    public List<RecipeDish> selectRandRecipeDish(RecipeDish recipeDish) {
        return recipeDishMapper.selectRandRecipeDish(recipeDish);
    }

    @Override
    public List<RecipeDish> selectAllocatedList(RecipeDish recipeDish) {
        return recipeDishMapper.selectAllocatedList(recipeDish);
    }

    @Override
    public List<RecipeDish> selectUnallocatedList(RecipeDish recipeDish) {
        return recipeDishMapper.selectUnallocatedList(recipeDish);
    }

    @Override
    public int selectMaxDishSort(RecipeDish recipeDish) {
        return recipeDishMapper.selectMaxDishSort(recipeDish);
    }

    @Override
    public boolean checkDishNameUnique(RecipeDish recipeDish) {
        Long id = StringUtils.isNull(recipeDish.getDishId()) ? -1L : recipeDish.getDishId();
        RecipeDish info = recipeDishMapper.checkDishNameUnique(recipeDish.getDishName());
        if (StringUtils.isNotNull(info) && info.getDishId().longValue() != id.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 新增菜品
     *
     * @param recipeDish 菜品
     * @return 结果
     */
    @Override
    public int insertRecipeDish(RecipeDish recipeDish) {
        recipeDish.setCreateTime(DateUtils.getNowDate());
        return recipeDishMapper.insertRecipeDish(recipeDish);
    }

    @Override
    public int batchInsertRecipeDish(List<RecipeDish> list) {
        return recipeDishMapper.batchInsertRecipeDish(list);
    }

    @Override
    public int batchInsertOrUpdateRecipeDish(List<RecipeDish> list) {
        return recipeDishMapper.batchInsertOrUpdateRecipeDish(list);
    }

    /**
     * 修改菜品
     *
     * @param recipeDish 菜品
     * @return 结果
     */
    @Override
    public int updateRecipeDish(RecipeDish recipeDish) {
        recipeDish.setUpdateTime(DateUtils.getNowDate());
        return recipeDishMapper.updateRecipeDish(recipeDish);
    }

    /**
     * 更新菜品的标题
     *
     * @param list 菜品ID集合
     * @return 结果
     */
    @Override
    public int updateDishTitle(List<Long> list) {
        return recipeDishMapper.updateDishTitle(list);
    }

    @Override
    public int updateDishTitle(String dishIds) {
        //字符串转list
        String[] idArray = dishIds.split(",");
        List<Long> list = Arrays.stream(idArray).map(Long::valueOf).collect(Collectors.toList());
        return this.updateDishTitle(list);
    }

    /**
     * 更新菜品点赞数
     *
     * @param dishIds 菜品ID数组
     * @param num     点赞数，1为增加，-1为减少
     * @return 结果
     */
    @Override
    public int updateLikesCount(Long[] dishIds, int num) {
        return recipeDishMapper.updateLikesCount(dishIds, num);
    }

    /**
     * 批量删除菜品
     *
     * @param dishIds 需要删除的菜品主键
     * @return 结果
     */
    @Override
    public int deleteRecipeDishByDishIds(String dishIds) {
        return recipeDishMapper.deleteRecipeDishByDishIds(Convert.toStrArray(dishIds));
    }

    /**
     * 删除菜品信息
     *
     * @param dishId 菜品主键
     * @return 结果
     */
    @Override
    public int deleteRecipeDishByDishId(Long dishId) {
        return recipeDishMapper.deleteRecipeDishByDishId(dishId);
    }
}
